/**
 * 工具包
 * 
 * */
export default {
	/**
	 *@description  图片等比例高度
	 */
	imageCalculate({width=750,e}={}) {
		let imgwidth = e.detail.width,
			imgheight = e.detail.height;
		//宽高比  
		let ratio = imgwidth / imgheight;
		return width / ratio;
	},
	/**
	 * @description  根据时间戳计算年龄
	 */
	getAgeDate(birthStr) {
		var birthDay = new Date(birthStr).getTime();
		var now = new Date().getTime();
		var hours = (now - birthDay) / 1000 / 60 / 60;
		var year = Math.floor(hours / (24 * 30 * 12));
		hours = hours % (24 * 30 * 12);
		var months = Math.floor(hours / (24 * 30));
		hours = hours % (24 * 30);
		var days = Math.floor(hours / (24));
		return {
			year: year,
			months: months,
			days: days,
		}
	},
	/** 
	 * 随机数
	 * @param {Object} minNum
	 * @param {Object} maxNum
	 */
	randomNum(minNum, maxNum) {
		switch (arguments.length) {
			case 1:
				return parseInt(Math.random() * minNum + 1, 10);
				break;
			case 2:
				return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
				//或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
				break;
			default:
				return 0;
				break;
		}
	},
	/*
	
	 * 将时间戳转时间格式
	 * value 时间戳
	 * fmt  时间格式
	 */
	formatDate({
		date,
		fmt = 'yyyy-MM-dd hh:mm:ss'
	} = {}) {

		if (date.length == 13) {
			date = date / 1000;
		}
		date = new Date(date);
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1,
				(date.getFullYear() + '').substr(4 - RegExp.$1.length));
		}
		let o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds()
		};
		for (let k in o) {
			if (new RegExp(`(${k})`).test(fmt)) {
				let str = o[k] + '';
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ?
					str : ('00' + str).substr(str.length));
			}
		}
		return fmt;
	},
	/**
	 * @description 获取当前时间
	 */
	getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var seperator2 = ":";
		var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
		var strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
		var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
		return currentdate;
	},
	/**
	 * @description 获取一段随机字符串
	 * @param {Object} len
	 */
	randomFileName(len) {
		len = len || 20;
		let $chars = 'ABCDEFG6888HJKMNPQRSTWXY156875Zabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
		let maxPos = $chars.length;
		let nameText = '';
		for (let i = 0; i < len; i++) {
			nameText += $chars.charAt(Math.floor(Math.random() * maxPos));
		}
		return nameText;
	},
	/**
	 * 获取文件的后缀名
	 * filName 图片的地址
	 */
	fileSuffix(filName) {
		if (!filName || typeof filName != 'string') {
			return false
		};
		let index = filName.lastIndexOf(".") - 1;
		let suffix = filName.substr(index + 1);
		return suffix;
	},
	/**
	 * @description  计算时间区间
	 * @param {Object} dateTimeStamp
	 */

	getDateDiff(dateTimeStamp) {
		var result;
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var diffValue = (new Date()) - dateTimeStamp;
		if (diffValue < 0) {
			return;
		}
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var minC = diffValue / minute;
		if (monthC >= 1) {
			if (monthC <= 12)
				result = "" + parseInt(monthC) + "月前";
			else {
				result = "" + parseInt(monthC / 12) + "年前";
			}
		} else if (weekC >= 1) {
			result = "" + parseInt(weekC) + "周前";
		} else if (dayC >= 1) {
			result = "" + parseInt(dayC) + "天前";
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前";
		} else if (minC >= 1) {
			result = "" + parseInt(minC) + "分钟前";
		} else {
			result = "刚刚";
		}
		return result;
	},
	/**
	 * @description 判断字符串是否是中文  true 是中文  false：不是中文
	 * @param {Object} str
	 */
	judgeChinese(str) {
		if (escape(str).indexOf('%u') < 0) {
			return false;
		} else {
			return true;
		}
	},

	/**
	 *@description  手机正则验证  true 手机正确  fasle手机失败
	 */
	regularPhone(n) {
		return (/^1[3456789]\d{9}$/.test(n));
	},
	/**
	 * @description  身份证正则验证  true 正确  fasle失败
	 */
	regularIdCard(n) {
		return (/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(n));
	},
	
	//方法:用来提取code
	getUrlCode(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1]
						.replace(/\+/g, '%20')) || null
	},
}
